package com.warom.sdg.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.warom.sdg.database.SdgDatabase;
import com.warom.sdg.model.VentilationConfigEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * VentilationConfigDao的实现类，使用原生SQLite API而不是Room
 */
public class VentilationConfigDao_Impl extends VentilationConfigDao {

    private static final String TAG = "VentilationConfigDao_Impl";
    private final SdgDatabase dbHelper;

    public VentilationConfigDao_Impl(SdgDatabase dbHelper) {
        super(dbHelper);
        this.dbHelper = dbHelper;
    }

    @Override
    public void upsert(VentilationConfigEntity config) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(SdgDatabase.COLUMN_VC_DEVICE_TYPE, config.getDeviceType());
        values.put(SdgDatabase.COLUMN_VC_TARGET_TEMPERATURE, config.getTargetTemperature());
        values.put(SdgDatabase.COLUMN_VC_TARGET_HUMIDITY, config.getTargetHumidity());
        values.put(SdgDatabase.COLUMN_VC_START_TIME, config.getStartTime());
        values.put(SdgDatabase.COLUMN_VC_END_TIME, config.getEndTime());
        values.put(SdgDatabase.COLUMN_VC_DURATION, config.getDuration());

        long result = db.insertWithOnConflict(SdgDatabase.TABLE_VENTILATION_CONFIG, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        if (result == -1) {
            Log.e(TAG, "Failed to upsert ventilation config for " + config.getDeviceType());
        } else {
            Log.i(TAG, "Successfully upserted ventilation config for " + config.getDeviceType());
        }
    }

    @Override
    public List<VentilationConfigEntity> getAll() {
        List<VentilationConfigEntity> configList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        try {
            cursor = db.query(SdgDatabase.TABLE_VENTILATION_CONFIG, null, null, null, null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                do {
                    VentilationConfigEntity config = new VentilationConfigEntity();
                    config.setDeviceType(cursor.getString(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_DEVICE_TYPE)));
                    config.setTargetTemperature(cursor.getInt(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_TARGET_TEMPERATURE)));
                    config.setTargetHumidity(cursor.getInt(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_TARGET_HUMIDITY)));
                    config.setStartTime(cursor.getString(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_START_TIME)));
                    config.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_END_TIME)));
                    config.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(SdgDatabase.COLUMN_VC_DURATION)));
                    configList.add(config);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            Log.e(TAG, "Error while trying to get all ventilation configs", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return configList;
    }
} 